package com.qyhl.webtv.module_circle.circle.home;


import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/1/17.
 */

public class CircleHomeModel implements CircleHomeContract.CircleModel {
    private CircleHomePresenter mPresenter;

    public CircleHomeModel(CircleHomePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取话题
     */
    @Override
    public void getTopic() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=tagList"
                )).isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleHomeBean.TagList>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.topicError(2, CircleUtils.getCircleMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(List<CircleHomeBean.TagList> tagLists) {
                        if (tagLists != null) {
                            mPresenter.topicSuccess(tagLists);
                        } else {
                            mPresenter.topicError(1, "暂无任何话题！");
                        }
                    }
                });


    }
}
